package cn.wolfcode.trip.admin.controller;


import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {

    @Autowired
    private IStrategyDetailService strategyDetailService;
    @Autowired
    private IStrategyService strategyService;

    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
        //根据攻略和序列排序
        qo.setOrderBy("sc.strategy_id,sc.sequence");
        model.addAttribute("result", strategyDetailService.queryForList(qo));
        model.addAttribute("strategies", strategyService.listEnable());
        return "strategyDetail/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(StrategyDetail strategyDetail, MultipartFile pic) {
        if (pic!=null && pic.getContentType().startsWith("image") && pic.getSize()>0){
            String url = UploadUtil.upload(pic);
            strategyDetail.setCoverUrl(url);
        }

        strategyDetailService.saveOrUpdate(strategyDetail);
        return "redirect:/strategyDetail/list.do";
    }

    @RequestMapping("/listCatalogBystrategyId")
    @ResponseBody
    public Object listCatalogBystrategyId(Long strategyId) {
        return strategyDetailService.listCatalogBystrategyId(strategyId);
    }

    @RequestMapping("/getContent")
    @ResponseBody
    public Object getContent(Long id) {
        return strategyDetailService.getContent(id);
    }


}
